package my2Cents.properties;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JLabel;

import my2Cents.Sandbox;

public class KeyAssign extends Property {
	private JButton doit;
	private JLabel tlabel;
	private boolean waiting = false;
	private int code = -1;
	private boolean press = false;
	private JButton clear;
	
	private boolean blocked(int code) {
		if(code == 32) return true;
		if(code == 27) return true;
		if(code == 82) return true;
		if(code == 90) return true;
		return false;
	}
	
	private String translateCode(int code) {
		if(code == 10) return "ENTER";
		if(code == 37) return "LEFT ARROW";
		if(code == 38) return "UP ARROW";
		if(code == 39) return "RIGHT ARROW";
		if(code == 40) return "DOWN ARROW";
		if(code == 16) return "SHIFT";
		if(code == 155) return "INSERT";
		if(code == 36) return "HOME";
		if(code == 33) return "PGUP";
		if(code == 34) return "PGDN";
		if(code == 35) return "END";
		if(code == 127) return "DEL";
		if(code == 112) return "F1";
		if(code == 113) return "F2";
		if(code == 114) return "F3";
		if(code == 115) return "F4";
		if(code == 116) return "F5";
		if(code == 118) return "F7";
		if(code == 120) return "F9";
		if(code == 121) return "F10";
		if(code == 122) return "F11";
		if(code == 123) return "F12";
		if(code == 144) return "NUM LOCK";
		if(code == 145) return "SCROLL LOCK";
		if(code == 20) return "CAPS LOCK";
		if(code == 8) return "BACKSPACE";
		if(code == 192) return "~";
		if(code == 17) return "CTRL";
		if(code == 18) return "ALT";
		return "";
	}
	
	public KeyAssign(final String label) {
		doit = new JButton("Assign");
		clear = new JButton("Clear");
		tlabel = new JLabel();
		tlabel.setText(label + " [<unbound>]");
		add(clear);
		add(doit);
		add(tlabel);
		centerY(tlabel);
		centerY(doit);
		centerY(clear);
		final KeyAssign me = this;
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				valueChanged(-1, ' ');
				tlabel.setText(label + " [<unbound>]");
				Sandbox.getToolMenu().lock(false);
				press = false;
				waiting = false;
			}
		});
		doit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tlabel.setText(label + " [Press Any Key]");
				waiting = true;
				Sandbox.getToolMenu().lock(true);
				//KeyAssignPopup pop = new KeyAssignPopup();
				//pop.setVisible(true);
			}
		});
		doit.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				if(waiting && arg0.getKeyChar() != 'q') {
					press = true;
				}
			}

			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub		
				if(waiting && press && arg0.getKeyChar() != 'q') {
					if(!blocked(arg0.getKeyCode())) {
						String lab = translateCode(arg0.getKeyCode());
						if(lab.equals("")) lab = ""+arg0.getKeyChar();
						tlabel.setText(label + " ["+lab+"] - "+arg0.getKeyCode());
						code = arg0.getKeyCode();
						//me.requestFocus();
						press = false;
						waiting = false;
						valueChanged(code, arg0.getKeyChar());
					}
					Sandbox.getToolMenu().lock(false);
				}
				if(arg0.getKeyChar() == 'q' && waiting == false) {
					Sandbox.getToolMenu().close();
				}
			}

			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub

			}
			
		});
	}
	
	public void validate() {
		super.validate();
		if(getParent() != null) {
			int w = getParent().getWidth();
			this.setSize(w,30);
			doit.setSize(100,20);
			clear.setSize(60,20);
			tlabel.setSize(w,30);
			doit.setLocation(w - 180, 0); 
			clear.setLocation(w - 70, 0);
			tlabel.setLocation(10, 0);
			centerY(tlabel);
			centerY(doit);
			centerY(clear);
		}
	}
	
	public void valueChanged(int value, char ch) {
		
	}

}
